package com.weilus;


import com.alibaba.cloud.spring.boot.sms.ISmsService;
import com.aliyun.oss.OSSClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Collections;

@RestController
@Slf4j
@EnableBinding(StreamChannel.class)
public class AliController {

    @Autowired
    private ISmsService smsService;
    @Autowired
    private OSSClient ossClient;
    @Autowired
    private StreamChannel source;

    @Autowired
    private ReactorLoadBalancerExchangeFilterFunction lbFunction;
    @Autowired
    private ReactiveCircuitBreakerFactory cbFactory;

    @GetMapping("sentinelTest")
    public Mono<String> sentinel(){
        return WebClient.builder()
                .filter(lbFunction)
                .build()
                .post()
                .uri("http://flux-service/api/hiMan")
                .bodyValue(Collections.singletonMap("name","科比"))
                .retrieve()
                .bodyToMono(String.class)
                .transform(it->cbFactory.create("aaaaaa").run(it,ex->Mono.just("sentinel fallback")));
    }

    /**
     * 阿里云对象存储
     * https://github.com/alibaba/aliyun-spring-boot/blob/master/aliyun-spring-boot-samples/aliyun-oss-spring-boot-sample/README-zh.md
     */
    @RequestMapping("/upload")
    public Mono<String> home() throws FileNotFoundException {
        ossClient.putObject("bucketName", "fileName", new FileInputStream("/your/local/file/path"));
        return Mono.just("upload success");
    }

    /**
     * 阿里云短信
     * https://github.com/alibaba/aliyun-spring-boot/blob/master/aliyun-spring-boot-samples/aliyun-sms-spring-boot-sample/READMD-zh.md
     */
    @PostMapping("/sms")
    public SendSmsResponse sms(@RequestParam(name = "code") String code) {
        SendSmsRequest request = new SendSmsRequest();
        // Required:the mobile number
        request.setPhoneNumbers("152******");
        // Required:SMS-SignName-could be found in sms console
        request.setSignName("******");
        // Required:Template-could be found in sms console
        request.setTemplateCode("******");
        // Required:The param of sms template.For exmaple, if the template is "Hello,your verification code is ${code}". The param should be like following value
        request.setTemplateParam("{\"code\":\"" + code + "\"}");
        try {
            return smsService.sendSmsRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
            return new SendSmsResponse();
        }
    }



    @GetMapping("testStream")
    public Mono<String> testStream(){
        this.source.output().send(MessageBuilder
                .withPayload("这是测试消息！！")
                .setHeader(RocketMQHeaders.TAGS, "tag3")
                .build());
        return Mono.just("SUCCESS");
    }



}
